<template>
  <div class="login">
    <div class="login_box">
      <h4>用户登录</h4>

      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="user">
          <el-input
            type="text"
            class="ipt"
            v-model="ruleForm.user"
            autocomplete="off"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>

        <el-form-item prop="pass">
          <el-input
            type="password"
            class="ipt"
            v-model="ruleForm.pass"
            autocomplete="off"
          >
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            ></el-input
          >
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex';
import { LoginRuder } from "../api";
import {setToken} from '../utils/token'
export default {
  name: "Login",
  data() {
    return {
      userInfo:{},
      ruleForm: {
        pass: "",
        user: "",
      },
      rules: {
        pass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, message: "长度在最少5个字符", trigger: "blur" },
        ],
        user: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, message: "长度在 最少 3 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapMutations(['GetName']),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { user, pass } = this.ruleForm;
          LoginRuder(user, pass).then((res) => {
            console.log(res);
            if (res.data.meta.status == 200) {
              this.userInfo = res.data.data
              console.log(res.data.data)
              setToken(res.data.data.token)
              this.GetName(res.data.data.username)
              this.$message({
                message: "登录成功",
                type: "success",
              });
              this.$router.replace({name:'home'})
            }else{
                this.$message({
                message: res.data.meta.msg,
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang='less' scoped>
.login {
  width: 100%;
  height: 100vh;
  background: #2d3a4b;
  overflow: hidden;
}
.login_box {
  width: 330px;
  height: 300px;
  margin: 150px auto;
}

h4 {
  color: #fff;
  text-align: center;
  font-size: 20px;
  font-weight: 400;
  margin-bottom: 15px;
}
/deep/ .el-form-item__content {
  margin-left: 0 !important;
}
/deep/ .el-button {
  width: 100% !important;
}
</style>